package com.mediacloud.app.newsmodule.activity.microlive;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.mediacloud.app.model.news.ArticleItem;
import com.mediacloud.app.model.news.CatalogItem;
import com.mediacloud.app.model.news.kt.TypeX;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.utils.NewsItemClickUtils;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 */

public class JJYYGridAdapter extends BaseRecyclerAdapter<String>
{
    public ArticleItem articleItem;
    public JJYYGridAdapter(Context context) {
        super(context);
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View view=LayoutInflater.from(context).inflate(R.layout.cggriditemsb,parent,false);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try
                {
                    int index=0;
                    try {
                        index=Integer.valueOf(v.getTag()+"");
                    }
                    catch (Exception e)
                    {
                    }
                    if (getItemCount() > 0 && v.getContext()!=null)
                    {
                        JSONArray images = new JSONArray();
                        for (String imgItem: getData())
                        {
                            JSONObject item=new JSONObject();
                            item.put("src",imgItem);
                            item.put("info",articleItem!=null?articleItem.getTitle():"");
                            images.put(images.length(),item);
                        }
                        final ArticleItem imgArticle = new ArticleItem();
                        imgArticle.setTitle(articleItem!=null?articleItem.getTitle():"");
                        imgArticle.setImage(images.toString());
                        imgArticle.setType(TypeX.NEWS.PHOTOS);
                        NewsItemClickUtils.OpenItemNewsHandle(v.getContext(), TypeX.NEWS.PHOTOS, imgArticle, new CatalogItem(), index);
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }

            }
        });
        return new SBZGNineGridHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        holder.itemView.setTag(position);
        SBZGNineGridHolder sbzgNineGridHolder= (SBZGNineGridHolder) holder;
        sbzgNineGridHolder.updateData(getItem(position));
    }
}